<include file="public/layout" />
<body style="background-color: #FFF; overflow: auto;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
	<div class="fixed-bar">
		<div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表"><i class="fa fa-arrow-circle-o-left"></i></a>
			<div class="subject">
				<h3>权限资源管理 - 编辑权限</h3>
				<h5>网站系统权限资源管理</h5>
			</div>
		</div>
	</div>
	<form class="form-horizontal" id="adminHandle" method="post">
		<input type="hidden" name="type" value="{$_GET[type]}">
		<input type="hidden" name="id" value="{$info.id}">
		<div class="ncap-form-default">
			<form id="typeForm" class="form-horizontal">
				<dl class="row">
					<dt class="tit">
						<label><em>*</em>所属类型</label>
					</dt>
					<dd class="opt">
						<select  id="type-select" name="type" onchange="handleTypeChange()">
							<volist name="modules" id="module">
								<option value="{$key}" <if condition="$Request.param.type==$key">selected</if>>{$module.title} - {$module.name}</option>
							</volist>
						</select>
					</dd>
				</dl>
			</form>
			<dl class="row">
				<dt class="tit">
					<label for="name"><em>*</em>权限资源名称</label>
				</dt>
				<dd class="opt">
					<input type="text" value="{$info.name}" name="name" id="name" class="input-txt">
					<span class="err">权限资源名称不能为空</span>
					<p class="notic"></p>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<label for="group"><em>*</em>所属分组</label>
				</dt>
				<dd class="opt">
					<select id="group" name="group">
						<foreach name="group" item="vo" key="key">
							<option value="{$key}" <if condition="$info[group] eq $key">selected</if>>{$vo}</option>
						</foreach>
					</select>
					<span class="err"></span>
					<p class="notic">所属分组</p>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<label for="group"><em>*</em>添加权限码</label>
				</dt>
				<dd class="opt">
					<select class="small form-control" id="controller" onchange="get_act_list(this)">
						<option value="">选择控制器</option>
						<foreach name="planList" item="vo">
							<option value="{$vo}">{$vo}</option>
						</foreach>
					</select>
					<span class="err">@</span>
					<ul class="ncap-account-container-list">
					</ul>
				</dd>
			</dl>
			<dl class="row">
				<dt class="tit">
					<label for="name"><em>*</em>权限码</label>
				</dt>
				<dd class="opt">
					<table>
						<tr><th style="width:80%">权限码</th><th style="width: 50px;text-align: center;" >操作</th></tr>
						<tbody id="rightList">
						<foreach name="info[right]" item="vo">
							<tr id="{:str_replace('@','_',$vo)}">
                                <td><input name="right[]" type="text" value="{$vo}" class="form-control" style="width:300px;"></td>
								<td style="text-align: center;"><a class="ncap-btn" href="javascript:;" onclick="$(this).parent().parent().remove();">删除</a></td>
                            </tr>
						</foreach>
						</tbody>
					</table>
				</dd>
			</dl>
			<div class="bot"><a href="JavaScript:void(0);" onclick="adsubmit();" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a></div>
		</div>
	</form>
</div>
<script type="text/javascript">
    function chkbox_bind(){
        $('input:checkbox').change(function () {
            var is_check = $(this).prop('checked');
            var ncode = $('#controller').val();
            var row_id = ncode+'_'+ $(this).val();
            if(is_check){
                var a = [];
                $('#rightList .form-control').each(function(i,o){
                    if($(o).val() != ''){
                        a.push($(o).val());
                    }
                });
                if(ncode !== ''){
                    var temp = ncode+'@'+ $(this).val();
                    if($.inArray(temp,a) != -1){
                        return ;
                    }
                }else{
                    layer.alert("请选择控制器" , {icon:2,time:1000});
                    return;
                }
                var strtr = "<tr id="+row_id+">";
                if(ncode!= ''){
                    strtr += '<td><input type="text" name="right[]" value="'+ncode+'@'+ $(this).val()+'" class="form-control" style="width:300px;"></td>';
                }else{
                    strtr += '<td><input type="text" name="right[]" value="" class="form-control" style="width:300px;"></td>';
                }
                strtr += '<td style="text-align: center;"><a href="javascript:;" class="ncap-btn" onclick="$(this).parent().parent().remove();">删除</a></td>';
                $('#rightList').append(strtr);
            }else{
                $("#"+row_id).remove();
            }
        });
    }
    chkbox_bind();
    function get_act_list(obj){
        $.ajax({
            url: "{:U('system/ajax_get_action',array('type'=>$_GET[type]))}",
            type:'get',
            data: {'controller':$(obj).val()},
            dataType:'html',
            success:function(res){
                $('.ncap-account-container-list').empty().append(res);
                chkbox_bind();
                updateActCheck();
            }
        });
    }
    function updateActCheck() {
        var acts = $('input.form-control');
        var controller = $('#controller').val();
        $('input:checkbox').each(function(){
            var act = controller +'@'+ $(this).val();
            for (var i = 0; i < acts.length; i++) {
                if ($(acts[i]).val() === act) {
                    $(this).attr('checked', true);
                    break;
                }
            }
        });
    }
    function adsubmit(){
        if($('input[name=name]').val() == ''){
            layer.msg('权限名称不能为空！', {icon: 2,time: 1000});
            return false;
        }

        if($('input[name="right[]"').val() == ''){
            layer.msg('权限码不能为空！', {icon: 2,time: 1000});
            return false;
        }

        $('#adminHandle').submit();
    }

    function handleTypeChange() {
        var type = $('#type-select').val();
        window.location.href = "{:U(edit_right)}/type/" + type;
    }
</script>
</body>
</html>